const Router = require('koa-router')
const router = new Router()
const dayjs = require('dayjs')
const path = require('path')
const fs = require('fs')
// const uuid = require('node-uuid')
const sourcePath = path.resolve(__dirname, '../source/data.json')
const categoriesPath = path.resolve(__dirname, '../source/categories.json')
// let data = require(sourcePath);
router.get('/getList', ctx => {
    const data = require(sourcePath);
    delete require.cache[sourcePath];
    ctx.body = { total: data.length, result: data }
})

router.get('/getCategories', ctx => {
    const data = require(categoriesPath);
    delete require.cache[categoriesPath];
    ctx.body = { total: data.length, result: data }

})

router.post('/addCategories', ctx => {
    const postData = ctx.request.body;
    let id = postData.id;
    const data = require(categoriesPath);
    delete require.cache[categoriesPath];
    if (postData.id) {
        data = data.map((item, index) => {
            if (item.id == postData.id) {
                postData.last_modify = dayjs().format('YYYY-MM-DD HH:ss:mm')
                return postData
            }
            return item
        })
    } else {
        id = dayjs().valueOf()
        postData.id = id
        postData.create_at = dayjs(id).format('YYYY-MM-DD HH:ss:mm')
        postData.last_modify = dayjs(id).format('YYYY-MM-DD HH:ss:mm')
        data.unshift(postData)
    }
    fs.writeFileSync(sourcePath, JSON.stringify(data))
    ctx.body = { id, status: 'success' }
})

router.post('/deleteCategories', async ctx => {
    const postData = ctx.request.body;
    if (!postData.id) {
        ctx.body = { status: 'error', msg: "id is not defined" }
    }
    const data = require(categoriesPath);
    delete require.cache[categoriesPath];
    const newData = data.filter(item => item.id != postData.id)
    fs.writeFileSync(sourcePath, JSON.stringify(newData))
    ctx.body = { status: 'success', id: postData.id }
})



router.post('/addList', async ctx => {
    const postData = ctx.request.body;
    let data = require(sourcePath);
    delete require.cache[sourcePath];
    let id = postData.id;

    if (postData.id && data.filter(item => item.id == postData.id).length > 0) {
        data = data.map((item, index) => {
            if (item.id == postData.id) {
                postData.last_modify = dayjs().format('YYYY-MM-DD HH:ss:mm')
                return postData
            }
            return item
        })
    } else {
        if (postData.key && data.filter(item => item.key == postData.key).length > 0) {
            ctx.body = { msg: "key 已存在", status: 'error' }
            return
        }
        id = dayjs().valueOf()
        postData.id = id
        postData.create_at = dayjs(id).format('YYYY-MM-DD HH:ss:mm')
        postData.last_modify = dayjs(id).format('YYYY-MM-DD HH:ss:mm')
        data.unshift(postData)
    }
    fs.writeFileSync(sourcePath, JSON.stringify(data))
    ctx.body = { id, status: 'success' }
})


router.post('/deleteList', async ctx => {
    const postData = ctx.request.body;
    if (!postData.id) {
        ctx.body = { status: 'error', msg: "id is not defined" }
    }
    const data = require(sourcePath);
    delete require.cache[sourcePath];
    const newData = data.filter(item => item.id != postData.id)
    fs.writeFileSync(sourcePath, JSON.stringify(newData))
    ctx.body = { status: 'success', id: postData.id }
})
module.exports = router